//package com.common.aop;
//
//import com.common.annotation.RedisCacheable;
//import com.common.cache.RedisClientTemplate;
//import org.apache.log4j.Logger;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.lang.annotation.Annotation;
//import java.lang.reflect.Method;
//import java.util.Arrays;
//
///**
// * Created by dell on 2016/8/5.
// */
//@Aspect
//@Component
//public class RedisCacheableAspect {
//
//    private static Logger logger = Logger.getLogger(RedisCacheableAspect.class);
//
//    private static String LOGHEAD="RedisCacheableAspect:";
//
//    @Autowired
//    private RedisClientTemplate redisClientTemplate;
//
//    @Pointcut("@annotation(com.common.annotation.RedisCacheable)")
//    public  void cacheAbleAspect() {
//
//    }
//
//    @Around("cacheAbleAspect()")
//    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//
//        String cacheKey = getCacheKey(joinPoint);
//        Object value=redisClientTemplate.get(cacheKey);
//        if(value!=null){
//            logger.info(LOGHEAD+"cache hit，key--"+ cacheKey);
//            return value;
//        }else {
//            Object result = joinPoint.proceed(joinPoint.getArgs());
//            RedisCacheable redisCacheable=getAnnotation(joinPoint,RedisCacheable.class);
//            redisClientTemplate.set(cacheKey,result);
//            logger.info(LOGHEAD+"put cache key--"+ cacheKey);
//            //设置过期时间
//            if(redisCacheable.expire()==-1){
//                //默认不设置过期
//                logger.info(LOGHEAD+"过期时间--永不过期");
//            }else {
//                redisClientTemplate.expire(cacheKey,redisCacheable.expire());
//                logger.info(LOGHEAD+"过期时间--"+redisCacheable.expire());
//            }
//            return result;
//        }
//    }
//
//    /**
//     * 根据类名、方法名和参数值获取唯一的缓存键
//     * @return 格式为 "包名.类名.方法名.参数类型.参数值"，类似 "your.package.SomeService.getById(int).123"
//     */
//    private String getCacheKey(ProceedingJoinPoint joinPoint) {
//        Object[] params=joinPoint.getArgs();
//        int kye=0;
//        kye= Arrays.hashCode(params);
////        try {
////            kye= Arrays.hashCode(params);// Coder.encryptMD5toString(params.toString());
////
////        } catch (NoSuchAlgorithmException e) {
////            e.printStackTrace();
////        }
//        String key= String.format("%s.%s",
//                joinPoint.getSignature().toString().split("\\s")[1], kye);
//        return key;
//    }
//
//    private <T extends Annotation> T getAnnotation(ProceedingJoinPoint jp, Class<T> clazz) {
//        MethodSignature sign = (MethodSignature) jp.getSignature();
//        Method method = sign.getMethod();
//        return method.getAnnotation(clazz);
//    }
//}
